<html>
<head><meta charset="utf-8"><title>How is exchange_malloc linked? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20is.20exchange_malloc.20linked.3F.html">How is exchange_malloc linked?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222456262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20is%20exchange_malloc%20linked%3F/near/222456262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Hughes <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20is.20exchange_malloc.20linked.3F.html#222456262">(Jan 12 2021 at 16:28)</a>:</h4>
<p>Hi. I'm curious how <code>exchange_malloc</code> is linked under the hood. IIUC the <code>exchange_malloc</code> lang item is annotated on a fn that calls the global allocator, and this lang_item is then referred to in the codegen / monomorphizer / mir etc. However, I notice that if I rename <code>fn exchange_malloc</code> to, for example, <code>fn foo</code> but keep all the lang items the same, ld complains about an <code>undefined reference to foo</code>. So I guess my question is why does this work when the fn and lang_item are both named <code>exchange_malloc</code> but not otherwise? Thanks</p>



<a name="222466774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20is%20exchange_malloc%20linked%3F/near/222466774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Hughes <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20is.20exchange_malloc.20linked.3F.html#222466774">(Jan 12 2021 at 17:34)</a>:</h4>
<p>Ah, could this be to do with bootstrapping perhaps? If I compile stage1 (where <code>exchange_malloc</code> -&gt; <code>foo</code>), then ld won't be able to find <code>foo</code> in stage0's liballoc.</p>



<a name="222467062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20is%20exchange_malloc%20linked%3F/near/222467062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20is.20exchange_malloc.20linked.3F.html#222467062">(Jan 12 2021 at 17:36)</a>:</h4>
<p>Bootstrapping shouldn't matter. Even an older rustc should just use the function name as stated in the libstd source (<code>foo</code>). It shouldn't have anything hard coded about the name. That is the whole purpose of the lang item system.</p>



<a name="222467563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20is%20exchange_malloc%20linked%3F/near/222467563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Hughes <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20is.20exchange_malloc.20linked.3F.html#222467563">(Jan 12 2021 at 17:40)</a>:</h4>
<p>I guess what I mean is that when building stage1, the lang_item for <code>exchange_malloc</code> will look for the fn <code>foo</code> in stage0's libstd, but this won't exist because it was built with an old version of rustc (where <code>exchange_malloc</code> was annotated on <code>fn exchange_malloc</code>).</p>



<a name="222467745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20is%20exchange_malloc%20linked%3F/near/222467745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20is.20exchange_malloc.20linked.3F.html#222467745">(Jan 12 2021 at 17:42)</a>:</h4>
<p>As I said, rustc shouldn't care about the name of it when it is compiled itself (<code>exchange_malloc</code>). It should only care about the name as stated in the libstd sources it compiled (<code>foo</code>).</p>



<a name="222467969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20is%20exchange_malloc%20linked%3F/near/222467969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Hughes <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20is.20exchange_malloc.20linked.3F.html#222467969">(Jan 12 2021 at 17:44)</a>:</h4>
<p>Hmm, I think I understand. This stuff is a bit confusing though</p>



<a name="222468273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20is%20exchange_malloc%20linked%3F/near/222468273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Hughes <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20is.20exchange_malloc.20linked.3F.html#222468273">(Jan 12 2021 at 17:46)</a>:</h4>
<p>I'm unclear why then that means that <code>./x.py build</code> leads to a linker error <code>undefined symbol to alloc::alloc::foo</code> when renaming the function which is annotated with the <code>exchange_malloc</code> lang_item.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>